package com.lan.viewObject;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.Map;

/**
 * 后端通用接口数据返回.
 *
 */
public final class Result extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /**
     * 返回属性定义.
     */
    public static final String CODE   = "code";        //返回结果，1正常、0失败
    public static final String STATUS = "status";      //返回http状态码，200成功、RCodeEnums系统自定义错误状态码、404资源找不到、500服务器内部错误等等
    public static final String MSG    = "msg";         //具体返回信息
    public static final String OBJ    = "obj";         //返回结果为obj单对象
    public static final String LIST   = "list";        //返回结果为list对象集合
    public static final String PAGE   = "page";        //返回结果为page分页对象
    public static final String MAP    = "map";         //返回结果为map

    /**
     * layUi单独的分页对象返回.
     **/
    public static final String DATA = "data";
    public static final String COUNT = "count";

    private static final String SUCCESS = "success";

    /**
     * 通用参数设置.
     **/
    public Result() {
        put(Result.CODE, 1);
        put(Result.STATUS, 200);
        put(Result.MSG, SUCCESS);
    }

    /**
     * 标准返回成功.
     **/
    public static Result ok() {
        return new Result();
    }

    /**
     * 标准返回成功 + 自定义消息.
     **/
    public static Result ok(final String msg) {
        Result result = new Result();
        result.put(MSG, msg);
        return result;
    }

    /**
     * 标准返回成功 + 自定义单个信息.
     **/
    public static Result ok(final String key, final Object value) {
        Result r = new Result();
        r.put(key, value);
        return r;
    }

    /**
     * 标准返回成功 + 自定义MAP信息.
     **/
    public static Result ok(final Map<String, Object> map) {
        Result r = new Result();
        r.putAll(map);
        return r;
    }

    /**
     * 后端接口自定义失败返回.
     * @param status http码
     * @param msg 消息说明
     * @return R 返回对象
     **/
    public static Result error(final Integer status, final String msg) {
        Result r = new Result();
        r.put(Result.CODE, 0);
        r.put(Result.STATUS, status);
        r.put(Result.MSG, msg);
        return r;
    }

    public static Result error(final String msg) {
        Result r = new Result();
        r.put(Result.CODE, 0);
        r.put(Result.STATUS, 0);
        r.put(Result.MSG, msg);
        return r;
    }

    /**
     * LayUi分页接口通用构造.
     * @param pager 自定义分页对象
     **/
    public static Result okForLayUi(final Pager pager) {
        Result r = new Result();
        r.put(Result.CODE, 0);
        r.put(Result.STATUS, 0);
        r.put(Result.DATA, pager.getList());
        r.put(Result.COUNT, pager.getTotalCount());
        r.put(Result.MSG, SUCCESS);
        return r;
    }

    /**
     * layUi分页成功接口通用构造.
     * @param pager mybatis-plus分页对象
     **/
    public static Result okForLayUi(final IPage<?> pager) {
        Result r = new Result();
        r.put(Result.CODE, 0);
        r.put(Result.STATUS, 0);
        r.put(Result.DATA, pager.getRecords());
        r.put(Result.COUNT, pager.getTotal());
        r.put(Result.MSG, SUCCESS);
        return r;
    }

    public static Result okForLayUi() {
        return new Result(true);
    }

    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }


    /**
     * 方法描述.
     * @param status http码
     * @param msg 消息说明
     **/
    public static Result errorForLayUi(final Integer status, final String msg) {
        Result r = new Result();
        r.put(Result.CODE, 1);
        r.put(Result.STATUS, status);
        r.put(Result.MSG, msg);
        return r;
    }
    /**
     * LayUi分页接口通用构造.
     * @param isLayUi 是否layUi的成功返回
     **/
    public Result(final boolean isLayUi) {
        put(Result.CODE, isLayUi?0:1);
        put(Result.STATUS, isLayUi?0:1);
        put(Result.MSG, isLayUi?"success":"error");
    }
}
